<?php

declare(strict_types=1);

namespace App\Fastapp\Job;

use App\Fastapp\Api\Service\Crypto\Coin\Ton;
use App\Fastapp\Api\Service\Crypto\PayService;
use App\Fastapp\Model\AccountWalletAddress;
use App\Fastapp\Model\AccountWalletAddressLog;
use Hyperf\AsyncQueue\Job;

class WalletAddressWithdrawalJob extends Job
{
    public function __construct(public int $id)
    {
    }

    public function handle(): void
    {
        $md = AccountWalletAddressLog::query()->where(['id' => $this->id, 'status' => 0])->first();
        if (!$md) return;
        try {
            if ($md->symbol_token) self::token($md);
            else self::coin($md);
        } catch (\Throwable $throwable) {
            sys_notify($throwable->getMessage() . '|' . $throwable->getTraceAsString());
        }
    }

    //代币
    public static function token(AccountWalletAddressLog $md): void
    {
        if ($md->symbol_token == 'USDT') self::usdt($md);
    }

    public static function usdt(AccountWalletAddressLog $log): void
    {
        $address = AccountWalletAddress::query()->where(['symbol' => $log->symbol, 'address' => $log->from])->first();
        if ($log->symbol == 'TON') {
//            try {
            if (Ton::transTonUsdt($address, $log->to, $log->amount)) return;
//            } catch (\Throwable) {}
            sys_notify('TONUSDT转账失败|' . json_encode($log->toArray()));
        } else if ($log->symbol == 'TRX') {
//            try {
            if ($txid = PayService::transTrxUsdt($address, $log->to, $log->amount)) {
                $log->update(['txid' => $txid, 'created_at' => date('Y-m-d H:i:s')]);
                return;
            }
//            } catch (\Throwable) {}
            sys_notify('TRC20转账失败|' . json_encode($log->toArray()));
        } else {
//            try {
            if ($txid = PayService::transEthUsdt($address, $log->to, $log->amount)) {
                $log->update(['txid' => $txid, 'created_at' => date('Y-m-d H:i:s')]);
                return;
            }
//            } catch (\Throwable) {}
            sys_notify('ERC20转账失败|' . json_encode($log->toArray()));
        }
    }

    //本币
    public static function coin(AccountWalletAddressLog $log): void
    {
        $address = AccountWalletAddress::query()->where(['symbol' => $log->symbol, 'address' => $log->from])->first();
        if ($log->symbol == 'TON') {
            try {
                $nonce = Ton::transTon($address, $log->to, $log->amount);
                if ($nonce !== false) {
                    $log->update(['txid' => $nonce, 'created_at' => date('Y-m-d H:i:s')]);
                    return;
                }
            } catch (\Throwable) {
            }
            sys_notify('TON转账失败|' . json_encode($log->toArray()));
        } elseif ($log->symbol == 'TRX') {
            try {
                if ($txid = PayService::transTrx($address, $log->to, $log->amount)) {
                    $log->update(['txid' => $txid, 'created_at' => date('Y-m-d H:i:s')]);
                    return;
                }
            } catch (\Throwable) {
            }
            sys_notify('TRX转账失败|' . json_encode($log->toArray()));
        } else {
//            try {
            if ($txid = PayService::transEth($address, $log->to, $log->amount)) {
                $log->update(['txid' => $txid, 'created_at' => date('Y-m-d H:i:s')]);
                return;
            }
//            } catch (\Throwable) {}
            sys_notify($log->symbol . '转账失败|' . json_encode($log->toArray()));
        }
    }

}
